package com.youlin.untils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.youlin.aop.WebControllerAop;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class SMSUtil {
    Logger logger = LoggerFactory.getLogger(WebControllerAop.class);

    @Value("${sms.regionId}")
    private String regionId;

    @Value("${sms.accessKeyId}")
    private String accessKeyId;

    @Value("${sms.secret}")
    private String secret;

    public JSONObject sendSms (String tel, Template template) {
        DefaultProfile profile = DefaultProfile.getProfile(this.regionId, this.accessKeyId, this.secret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("PhoneNumbers", tel);
        request.putQueryParameter("SignName", "中广视媒");
        request.putQueryParameter("TemplateCode", template.getCode());
        int randomCode = (int) ((Math.random() * 9 + 1) * 100000);
        request.putQueryParameter("TemplateParam", "{\"code\":"+randomCode+"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            String data = response.getData().replace("{", "{\"code\":"+randomCode+",");
            return JSON.parseObject(data);
        } catch (Exception e) {
            logger.error("sendSms ===>> ", e);
            throw new RuntimeException();
        }
    }

    public void noticeSms (String tel, Template template, String name, String uuid) {
        DefaultProfile profile = DefaultProfile.getProfile(this.regionId, this.accessKeyId, this.secret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("PhoneNumbers", tel);
        request.putQueryParameter("SignName", "中广视媒");
        request.putQueryParameter("TemplateCode", template.getCode());
        request.putQueryParameter("TemplateParam", "{\"name\":"+name+", \"uuid\":"+uuid+"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
        } catch (Exception e) {
            logger.error("noticeSms ===>> ", e);
            throw new RuntimeException();
        }
    }

    public enum Template {
        TAKERLOGIN ("承办人登录", "SMS_193105403"),
        TAKERNOTICE ("审核完成后通知承办人", "SMS_195862112");

        private String message;
        private String code;

        Template (String message, String code) {
            this.message = message;
            this.code = code;
        }

        public String getCode() {
            return code;
        }

    }

}

